<?php

//管理员
class Admin extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin_model');
	}

	public function index()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;
		
		$per_page = 20;

		$count = $this->admin_model->get_count();
		$admin_list = $this->admin_model->get_page($p, $per_page);
		
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('admin/index'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();

		$this->load->model('group_model');
		$group_list = $this->group_model->get_all();
		
		$this->template->assign('group_list', $group_list);
		$this->template->assign('list', $admin_list);
		$this->template->assign('page', $this->page);
		$this->template->display('admin/list');
	}

	//搜索
	public function search()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;

		$username = trim(get('username'));
		$email = trim(get('email'));
		$real_name = trim(get('real_name'));
		$group_id = intval(get('group_id'));
		$banned = intval(get('banned')) > 0 ? 1 : 0;
		
		$per_page = 20;

		$this->db->select('count(*) as count')->from('admin');

		$this->db->where('banned', $banned);

		if ($group_id > 0)
		{
			$this->db->where('group_id', $group_id);
		}

		if ($username)
		{
			$this->db->like('username', $username);
		}

		if ($email)
		{
			$this->db->like('email', $email);
		}

		if ($real_name)
		{
			$this->db->like('real_name', $real_name);
		}

		$result = $this->db->get()->row_array();
		$count = $result['count'];

		$this->db->select('admin.id, admin.username, admin.email, admin.banned, admin.group_id, group.name as group_name')->from('admin')->join('group', 'admin.group_id = group.id', 'left');

		$this->db->where('banned', $banned);

		if ($group_id > 0)
		{
			$this->db->where('group_id', $group_id);
		}

		if ($username)
		{
			$this->db->like('admin.username', $username);
		}

		if ($email)
		{
			$this->db->like('admin.email', $email);
		}

		if ($real_name)
		{
			$this->db->like('admin.real_name', $real_name);
		}

		$admin_list = $this->db->limit($per_page, ($p-1)*$per_page)->get()->result_array();

		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('admin/search'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();

		$this->load->model('group_model');
		$group_list = $this->group_model->get_all();
		
		$this->template->assign('group_list', $group_list);
		$this->template->assign('list', $admin_list);
		$this->template->assign('page', $this->page);
		$this->template->display('admin/list');
	}

	//添加管理员页面
	protected function _insert_get()
	{
		$this->load->model('group_model');
		$group_list = $this->group_model->get_all();
		
		$this->load->model('access_model');
		$access_list = $this->access_model->get_all();
		
		$this->template->assign('group_list', $group_list);
		$this->template->assign('access_list', $access_list);
		
		$this->template->display('admin/insert');
	}
	
	//提交添加管理员
	protected function _insert_post()
	{
		$this->load->helper('validation');
		
		$username = trim(post('username')); //用户名
		$password = trim(post('password')); //密码
		$email = trim(post('email')); //邮箱
		$real_name = trim(post('real_name')); //真实姓名
		$banned = intval(post('banned')) > 0 ? 1 : 0; //禁用状态
		$group_id = intval(post('group_id')); //分组id
		$access_list = post('access') ? implode(',', array_unique(array_map('intval', post('access')))) : ''; //权限
		
		if ($username == '')
		{
			msg('用户名必须填写！', -1);
		}
		elseif ( ! preg_match('/^[a-z0-9_]{1,20}$/i', $username))
		{
			msg('用户名必须由数字、字母、下划线组成！不得超过20个字符！', -1);
		}
		elseif ($password == '')
		{
			msg('密码必须填写！', -1);
		}
		elseif ( ! preg_match('/^[^\s]{5,20}$/', $password))
		{
			msg('密码必须由5-20个非空字符组成！', -1);
		}
		elseif ($email == '')
		{
			msg('邮箱必须填写！', -1);
		}
		elseif ( ! is_email($email))
		{
			msg('邮箱格式错误！', -1);
		}
		elseif ( ! $real_name)
		{
			msg('真实姓名必须填写！', -1);
		}
		elseif ($group_id < 1)
		{
			msg('请选择分组！', -1);
		}
		
		$this->load->model('group_model');
		if ( ! $this->group_model->exists($group_id))
		{
			msg('分组不存在！', -1);
		}
		
		if ( ! $this->admin_model->is_username_available($username))
		{
			msg('用户名已经存在！', -1);
		}
		
		$data = array(
			'group_id' => $group_id,
			'username' => $username,
			'password' => $this->admin_model->password_hash($password),
			'email' => $email,
			'real_name' => $real_name,
			'banned' => $banned,
			'access_list' => $access_list
		);
		
		if ( ! ($id = $this->admin_model->insert($data)))
		{
			log_message('error', '添加管理员失败！原因：写入admin表失败！');
			msg('添加管理员失败！');
		}
		
		$msg = '添加成功！<br /><br /><a href="'.admin_url('admin/update', array('id' => $id)).'" class="button">返回修改</a>&nbsp;&nbsp;&nbsp;<a href="'.admin_url('admin/insert').'" class="button">继续添加</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//修改页面
	protected function _update_get()
	{
		$id = intval(get('id'));
		if ($id < 1)
		{
			msg('非法访问！');
		}
		
		$admin_data = $this->admin_model->get($id);
		if ( ! $admin_data)
		{
			msg('账号不存在！', admin_url('admin/index'));
		}
		$admin_data['access_list'] = explode(',', $admin_data['access_list']);
		
		//管理员分组
		$this->load->model('group_model');
		$group_list = $this->group_model->get_all();
		
		$this->load->model('access_model');
		$access_list = $this->access_model->get_all();
		
		$this->template->assign('group_list', $group_list);
		$this->template->assign('access_list', $access_list);
		$this->template->assign('data', $admin_data);
		
		$this->template->display('admin/update');
	}
	
	//提交修改信息
	protected function _update_post()
	{
		$this->load->helper('validation');
		
		$id = intval(post('id')); //管理员id
		$password = trim(post('password')); //密码
		$email = trim(post('email')); //邮箱
		$real_name = trim(post('real_name')); //真实姓名
		$banned = in_array(intval(post('banned')), array(0, 1)) ? intval(post('banned')) : 0; //禁用状态
		$group_id = intval(post('group_id')); //分组id
		$access_list = post('access') ? implode(',', array_unique(array_map('intval', post('access')))) : ''; //权限
		
		if ($id < 1)
		{
			msg('非法访问！');
		}
		elseif ($group_id < 1)
		{
			msg('分组不存在！', -1);
		}
		elseif ($password && ! preg_match('/^[^\s]{5,20}$/', $password))
		{
			msg('密码必须由5-20个非空字符组成！', -1);
		}
		elseif ( ! $email)
		{
			msg('邮箱必须填写！', -1);
		}
		elseif ( ! is_email($email))
		{
			msg('邮箱格式错误！', -1);
		}
		elseif ( ! $real_name)
		{
			msg('真实姓名必须填写！', -1);
		}
		
		$admin_data = $this->admin_model->get($id);
		if ( ! $admin_data)
		{
			msg('账号不存在或已被删除！', admin_url('user/index'));
		}
		
		$this->load->model('group_model');
		if ( ! $this->group_model->exists($group_id))
		{
			msg('分组不存在！');
		}
		
		$data = array(
			'group_id' => $group_id,
			'email' => $email,
			'banned' => $banned,
			'real_name' => $real_name,
			'banned' => $banned,
			'access_list' => $access_list
		);
		
		if ($password)
		{
			$data['password'] = $this->admin_model->password_hash($password);
		}
		
		if (FALSE === $this->admin_model->update($data, $id))
		{
			log_message('error', '修改管理员信息失败！原因：更新admin表失败！');
			msg('修改失败！');
		}
		
		$msg = '修改成功！<br /><br /><a href="'.admin_url('admin/update', array('id' => $id)).'" class="button">继续修改</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//删除管理员
	public function _delete_ajax()
	{
		$id = intval(get('id'));
		
		if ($id < 1)
		{
			ajax(array('code' => 1, 'msg' => '非法访问！'));
		}
		elseif ($id == 1)
		{
			ajax(array('code' => 2, 'msg' => '此管理员不能删除！'));
		}
		
		$result = $this->admin_model->delete($id);

		if ($result !== FALSE)
		{
			ajax(array('code' => 0, 'msg' => '删除成功！'));
		}
		else
		{
			log_message('error', '删除管理员失败！原因：更新admin表失败！');
			ajax(array('code' => 3, 'msg' => '删除失败！'));
		}
	}
	
	//禁用状态改变
	public function banned()
	{
		$this->_banned_ajax();
	}
	
	//禁用状态改变
	protected function _banned_ajax()
	{
		$id = intval(get('id'));
		$banned = intval(get('banned')) > 0 ? 1 : 0;
		
		if ($id < 1)
		{
			ajax(array('code' => 1, 'msg' => '非法访问！'));
		}
		elseif ($id == 1)
		{
			ajax(array('code' => 2, 'msg' => '此管理员不能禁用！'));
		}
		
		$result = $this->admin_model->update(array('banned' => $banned), $id);
		
		if ($result !== FALSE)
		{
			ajax(array('code' => 0, 'msg' => '更新成功！'));
		}
		else
		{
			log_message('error', '更改管理员状态失败！原因：更新admin表失败！');
			ajax(array('code' => 3, 'msg' => '更新失败！'));
		}
	}
	
}